package com.wonderland.webtest.user.service;

import com.wonderland.webtest.user.dao.UserDao;
import com.wonderland.webtest.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by gaoxy on 2015-09-14 0014.
 */
@Service
@DependsOn("userDao")
public class UserService {
    @Autowired
    private UserDao userDao;

    public List<User> getUserList() {
//        userDao.addUser(new User("5","user1"));
//        String[] a = new String[3];
//        System.out.println(a[5]);
        return userDao.getUserList();
    }

    public List<Map> getDataSources() {
        return userDao.getDataSources();
    }

    private static String USER_COLLECTION = "user";

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * @param user
     */
    public void saveUser(User user) {
        mongoTemplate.save(user, USER_COLLECTION);
    }

    /**
     * @param name
     * @return
     */
    public User findUserByName(String name) {
        return mongoTemplate.findOne(new Query(Criteria.where("username").is(name)), User.class, USER_COLLECTION);
    }
}
